package com.cncytech.uiautomator_demo;

import android.util.Log;

import com.cncytech.uiautomator_demo.utils.Describe;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 参数化 监听器
 */
public class IListener4Parameters extends RunListener {
    private static final String TAG = "IListener4Parameters";

    public static Map<String, ResultBean> reslutContext = new HashMap<>();
    @Override
    public void testRunStarted(Description description) throws Exception {
        super.testRunStarted(description);
    }

    @Override
    public void testRunFinished(Result result) throws Exception {
        // super.testRunFinished(result);
        Log.i(TAG, "testRunFinished: ");
        reslutContext.forEach((k,v)->{
            if ("pass".equals(v.getReslut())) {
                Log.i(TAG, "testRunFinished: "+v);
            } else if ("failure".equals(v.getReslut())) {
                Log.e(TAG, "testRunFinished: "+v);
            }else {
                Log.w(TAG, "testRunFinished: "+v);
            }
        });
    }

    /**
     * key 需要重新想一下
     * @param description
     * @throws Exception
     */
    @Override
    public void testStarted(Description description) throws Exception {
        super.testStarted(description);
        Log.i(TAG, "testStarted: " + description);
        Describe annotation = description.getAnnotation(Describe.class);
        // TODO: 2024/1/26 只计算添加描述的测试用例，其他不计入测试结果
        if (Objects.nonNull(annotation)) {
            // TODO: 2024/1/26 如果是第一次运行，就添加 如果是重跑，就不添加
            if (!reslutContext.containsKey(description.getDisplayName())) {
                reslutContext.put(description.getDisplayName(), new ResultBean(annotation.value()));
            }
        }
    }

    @Override
    public void testFinished(Description description) throws Exception {
        Log.i(TAG, "testFinished: " + description);

        super.testFinished(description);
    }

    @Override
    public void testFailure(Failure failure) throws Exception {
        Log.i(TAG, "testFailure: " + failure.getDescription().getMethodName());
        super.testFailure(failure);
        ResultBean resultBean = reslutContext.get(failure.getDescription().getDisplayName());
        if ( Objects.nonNull(resultBean)) {
            resultBean.setReslut("failure");
            resultBean.setMsg(failure.getMessage());
            resultBean.setReslut(failure.getTrace());
            resultBean.setDisplay(failure.getTestHeader());
        }
    }

    @Override
    public void testAssumptionFailure(Failure failure) {
        super.testAssumptionFailure(failure);
    }

    @Override
    public void testIgnored(Description description) throws Exception {
        super.testIgnored(description);
        reslutContext.get(description.getDisplayName()).setMsg("ignore");
    }
}
